<!DOCTYPE html>
<html>
	<head>
		<title>设置个人资料</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<script src="/js/bootstrap/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/myInfo.css" />

		<script src="../js/plugins/jrender/jrender.min.js"></script>
		<script src="/js/plugins/jquery-form/jquery.form.js"></script>
	<script>
		$(function () {
		    //从登陆login.HTML中拿当前登录用户对象信息
            var user = JSON.parse(sessionStorage.getItem('user'));
            $('.info').renderValues(user);
            //muBg是bootStrap中的一个样式,修改背景,添加一个background-image样式,当前登录用户的背景墙
            $('.myBg').css("background-image","url('"+ user.coverImgUrl +"')")

			var flag;
			//给头像按钮绑定点击事件,进行头像上传
			$('#headBtn').click(function () {
				//cilck(func(){}): click()中若有函数,表示绑定点击事件
				//click(): 若没有函数,表示自动执行点击事件(默认的点击事件就是弹出文件选择窗口)
				$('#imageFile').click();
				flag = 1;
            });

            $('#coverBtn').click(function () {
                //cilck(func(){}): click()中若有函数,表示绑定点击事件
                //click(): 若没有函数,表示自动执行点击事件(默认的点击事件就是弹出文件选择窗口)
                $('#imageFile').click();
                flag = 2;
            });

            //如何感知到图片已经选择?
			//通过change()事件,内容改变事件确定是否已经选了图片
			//执行图片上传
            $('#imageFile').change(function () {
				//如果选择了图片,进行图片上传
                if(this.value){
                    //提交表单数据,使用AjaxSubmit提交
                    $('#imageForm').ajaxSubmit(function (data) {
						//图片上传成功
                        if(data.status == 1){
                            if(flag == 1){
                                //回显图片
                                $('#headImg').prop("src",data.url);
                                //修改最初的图片
                                $('#headImgUrl').val(data.url);
                            }else{
                                $('.myBg').css("background-image","url('"+ data.url +"')")
							    $('#coverImgUrl').val(data.url);
                            }
						}
                    })
				}
            });
			$('#saveBtn').click(function () {
				$('#infoForm').ajaxSubmit(function (data) {
					if(data.success){
					    alert(1);
                        sessionStorage.setItem('user', JSON.stringify(data.data));
					    window.location.href = "/index.html";
					}
                })
            });
		});
	</script>
	</head>

	<body>
		<!--图片上传表单,enctype表示表单类型-->
		<form action="/images" method="post" enctype="multipart/form-data" id="imageForm">
			<!--accept表示想要请求的类型时image图片类型-->
			<input type="file" name="file" accept="image/*" style="display: none" id="imageFile">
		</form>
		<div class="search-head">
			<div class="row nav-search">
				<div class="col">
					<a href="javascript:window.history.go(-1);">
						<span>取消</span>
					</a>
				</div>
				<div class="col">
					<span>设置个人资料</span>
				</div>
				<div class="col" >
					<a href="javascript:;" id="saveBtn" >
						<span>保存</span>
					</a>
				</div>
			</div>
		</div>

		<div class="container info">
			<div class="myBg" >
				<img id="headImg"  class="rounded-circle" render-src="headImgUrl"/>
				<div class="editHead" id="headBtn" ><i class="fa fa-pencil-square fa-lg" aria-hidden="true"></i></div>
				<button  class="btn btn-outline-light ibtn " id="coverBtn">设置背景</button>
			</div>

			<form id="infoForm" method="post" action="/users">
					<input type="hidden" name="id" render-value="id">
					<input type="hidden" name="_method" value="put">

					<input type="hidden" name="headImgUrl" id="headImgUrl" render-value="headImgUrl">
					<input type="hidden" name="coverImgUrl" id="coverImgUrl" render-value="coverImgUrl">

					<label>昵称</label><input type="text" name="nickName" class="form-control" render-value="nickName">
					<label>性别</label><select class="form-control" name="gender" render-value="gender">
							<option value="2">保密</option>
							<option value="0">男</option>
							<option value="1">女</option>
						</select>
					<label>地区</label><input type="text" name="place"  class="form-control" render-value="place">
					<label>签名</label><textarea name="sign" class="form-control" rows="5" render-value="sign"></textarea>
			</form>
		</div>
		
		
		
	</body>
</html>
